package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.web.bind.annotation.RequestMapping;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.*;
import com.itheima.service.RoleService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("role")
public class RoleController {
    @Reference
    private RoleService roleService;

    @RequestMapping("findAll")
    public Result findAll(){
        try {
            List<Role> roleList = roleService.findAll();
            return new Result(true,"查询成功",roleList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询失败");
        }
    }
    @RequestMapping("findRoleIdsByUserId")
    public Result findRoleIdsByUserId(@RequestParam("id")Integer id ){
        try {
            List<Integer> roleIds = roleService.findRoleIdsByUserId(id);
            return new Result(true, "查询角色列表成功",roleIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询角色列表成功");
        }
    }

    /**
     * @title findPage
     * @description 分页查询角色
     * @author juju
     * @updateTime 2020/6/29 18:58
     * @throws
     */
    @RequestMapping("findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){

        try {
            PageResult pageResult = roleService.findPage(queryPageBean);
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS,pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }
    }
    /**
     * @title add
     * @description 新增角色
     * @author juju
     * @updateTime 2020/6/29 20:18
     * @throws
     */
    @RequestMapping("add")
    public Result add(@RequestParam("keywordIds")Integer [] keywordIds, @RequestParam("menuIds")Integer [] menuIds, @RequestBody Role role){

        try {
            roleService.add(keywordIds,menuIds,role);
            return new Result(true, MessageConstant.ADD_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_ROLE_FAIL);
        }

    }
    /**
     * @title findPermission
     * @description 查询权限数据将其展示在新增角色前台
     * @author juju
     * @updateTime 2020/6/29 23:21
     * @throws
     */
    @RequestMapping("findPermission")
    public Result findPermission() {
        try {
            List<Permission> permissionList=roleService.findPermission();
            return new Result(true, MessageConstant.QUERY_PERMISSION_SUCCESS,permissionList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.QUERY_PERMISSION_FAIL);
        }
    }
    /**
     * @title findMenu
     * @description 查询所有菜单
     * @author juju
     * @updateTime 2020/6/29 23:31
     * @throws
     */
    @RequestMapping("findMenu")
    public Result findMenu() {
        try {
            List<Menu> menuList=roleService.findMenu();
            return new Result(true, MessageConstant.QUERY_MENU_SUCCESS,menuList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.QUERY_MENU_FAIL);
        }
    }
    /**
     * @title findPermissionIdsByRoleId
     * @description 根据角色id查询其拥有的权限id
     * @author juju
     * @updateTime 2020/6/30 9:14
     * @throws
     */
    @RequestMapping("findPermissionIdsByRoleId")
    public Result findPermissionIdsByRoleId(@RequestParam("id")Integer id) {
        try {
            List<Integer> keywordIds=roleService.findPermissionIdsByRoleId(id);
            return new Result(true, MessageConstant.QUERY_PERMISSION_SUCCESS,keywordIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_PERMISSION_FAIL);
        }
    }
    /**
     * @title findMenuIdsByRoleId
     * @description 根据角色id查询其拥有的菜单id
     * @author juju
     * @updateTime 2020/6/30 9:16
     * @throws
     */
    @RequestMapping("findMenuIdsByRoleId")
    public Result findMenuIdsByRoleId(@RequestParam("id")Integer id) {
        try {
            List<Integer> menuIds=roleService.findMenuIdsByRoleId(id);
            return new Result(true, MessageConstant.QUERY_MENU_SUCCESS,menuIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_MENU_FAIL);
        }
    }
    /**
     * @title edit
     * @description 角色的编辑
     * @author juju
     * @updateTime 2020/6/30 9:53
     * @throws
     */
    @RequestMapping("edit")
    public Result edit(@RequestParam("keywordIds")Integer [] keywordIds, @RequestParam("menuIds")Integer [] menuIds, @RequestBody Role role) {
        try {
            roleService.edit(keywordIds,menuIds,role);
            return new Result(true, MessageConstant.EDIT_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_ROLE_FAIL);
        }
    }
    /**
     * @title deleteById
     * @description 根据角色id删除角色
     * @author juju
     * @updateTime 2020/6/30 10:54
     * @throws
     */
    @RequestMapping("deleteById")
    public Result deleteById(@RequestParam("id")Integer id) {
        try {
            roleService.deleteById(id);
            return new Result(true, MessageConstant.DELETE_ROLE_SUCCESS);
        }catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false, e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_ROLE_FAIL);
        }
    }
}
